package com.dimenlink.admin.controller;

import com.dimenlink.entity.po.CategoryInfo;
import com.dimenlink.entity.query.CategoryInfoQuery;
import com.dimenlink.entity.vo.ResponseVO;
import com.dimenlink.service.CategoryInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController extends ABaseController{

    @Resource
    private CategoryInfoService categoryInfoService;

    @RequestMapping("/loadCategory")
    public ResponseVO loadCategory(CategoryInfoQuery query) {
        query.setOrderBy("sort asc");
        query.setCovert2Tree(true);
        List<CategoryInfo> categoryInfoList = categoryInfoService.findListByParam(query);
        return getSuccessResponseVO(categoryInfoList);
    }

    @RequestMapping("/saveCategory")
    public ResponseVO saveCategory(@NotEmpty Integer pCategoryId,
                                   Integer categoryId,
                                   @NotEmpty String categoryCode,
                                   @NotEmpty String categoryName,
                                   String icon,
                                   String background) {

    	CategoryInfo categoryInfo = new CategoryInfo();
        categoryInfo.setpCategoryId(pCategoryId);
        categoryInfo.setCategoryId(categoryId);
        categoryInfo.setCategoryCode(categoryCode);
        categoryInfo.setCategoryName(categoryName);
        categoryInfo.setIcon(icon);
        categoryInfo.setBackground(background);

        categoryInfoService.saveCategory(categoryInfo);
        return getSuccessResponseVO("添加成功");
    }

    @RequestMapping("/delCategory")
    public ResponseVO delCategory(@NotEmpty Integer categoryId) {
    	categoryInfoService.delCategory(categoryId);
    	return getSuccessResponseVO("删除成功");
    }

    @RequestMapping("/changeSort")
    public ResponseVO changeSort(@NotEmpty Integer pCategoryId,
    							 @NotEmpty String categoryIds) {
    	categoryInfoService.changeSort(pCategoryId, categoryIds);
        return getSuccessResponseVO("排序成功");
    }
}
